package com.atguigu.test.config;

import com.atguigu.test.boot.annotation.MyConditionalOnMissBean;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Map;

/**
 * @author zp
 * @version 1.0
 * @description: 自定义MyConditionalMissBean处理类
 * @date 2023/2/9 18:43
 */
public class MyConditionalOnMissClass implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Class<?> className = (Class<?>) metadata.getAnnotationAttributes(MyConditionalOnMissBean.class.getName()).get("className");
        Map<String, ?> beansOfType = context.getBeanFactory().getBeansOfType(className);
        return beansOfType.size()==0;
    }
}
